import { Component, OnInit } from '@angular/core';
import {Router} from '@angular/router';
import {AddressService} from '../../lib/service/address.service';
import {HttpService} from '../../lib/service/http.service';
import {adSystemApiUrl} from 'config/api';
import {LocalStorageService} from '../../lib/service/local-storage.service';

export class AddContact {
  fullname: string;
  mobile: number;
  qq: number;
  province = 0;
  city = 0;
  type = '意向客户';
}

@Component({
  selector: 'app-make',
  templateUrl: './make.component.html',
  styleUrls: ['./make.component.scss'],
  providers: [AddressService, LocalStorageService]
})
export class MakeComponent implements OnInit {

  type: string;
  message: string;
  isShow = false;

  provinceList = [];
  cityList = [];
  modelCase;
  addContact = new AddContact;

  constructor(private addressService: AddressService,
              private httpService: HttpService,
              private router: Router,
              private localStorageService: LocalStorageService) { }

  ngOnInit() {
    this.getProvinceList();
  }

  // 获取省列表
  getProvinceList() {
    this.addressService.getProvinceList().subscribe(
      res => {
        this.provinceList = res.data;
      }
    );
  }

  // 更改省之后
  changeProvince() {
    this.addContact.city = 0;
    this.addressService.getCityList(this.addContact.province).subscribe(
      res => {
        this.cityList = res.data;
      }
    )
  }

  // 意向客户提交联系方式
  contactSubmit() {
    if (!this.addContact.city) {
      this.type = 'info';
      this.message = '请选择您的所在地';
      this.isShow = true;
      return;
    }
    const url = `${adSystemApiUrl}/v1/fddata/applet_apply/add`;
    return this.httpService.httpPostAuth(url,this.addContact).subscribe(
      res => {
        if (res.code === 2000) {
          this.type = 'success';
          this.message = '提交成功';
          this.isShow = true;
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    )
  }

  turnPage() {
    const auth = this.localStorageService.getUserInfo();
    if (!auth) {
      this.type = 'info';
      this.message = '请先登录';
      this.isShow = true;
      return false;
    }
    this.router.navigate(['/main/index']);
  }
}
